package com.ta.service.impl;

import com.ta.mapper.UserMapper;
import com.ta.pojo.User;
import com.ta.service.UserService;
import com.ta.service.ex.InsertException;
import com.ta.service.ex.UpdateException;
import com.ta.service.ex.UserNameRepeatException;
import com.ta.service.ex.UserNotFoundException;
import com.ta.util.ThrowServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl extends ThrowServiceException implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 注册业务接口实现
     * @param user
     */
    @Override
    public void insert(User user) {
        User userName = userMapper.findByUserName(user.getUsername());
        if(userName!=null){
            throw new UserNameRepeatException("用户名重复");
        }

        String salt = UUID.randomUUID().toString();
        String md5PassWord = getMd5PassWord(user.getPassword(), salt);

        user.setPassword(md5PassWord);
        user.setSalt(salt);
        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        user.setCreatedTime(new Date());
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());

        Integer row = userMapper.insert(user);
        if(row!=1){
            throw new InsertException("注册用户时出现未知异常");
        }

    }

    /**
     * 登录业务接口实现
     * @param user
     */
    @Override
    public User login(User user) {
        User result = findByUser(user);

        String md5PassWord = getMd5PassWord(user.getPassword(), result.getSalt());
        if(!md5PassWord.equals(result.getPassword())){
            throw new UserNotFoundException("密码错误，请重新输入！");
        }
        return result;
    }

    /**
     * 修改密码业务接口实现
     * @param password  新密码
     * @param formerPassWord 旧密码
     * @param userName  当前用户名
     */
    @Override
    public void modifyPassWord(String password, String formerPassWord, String userName) {
        User user = userMapper.findByUserName(userName);
        if(user == null || user.getIsDelete().equals(1)){
            throw new UserNameRepeatException("用户不存在");
        }
        
        String md5PassWord = getMd5PassWord(formerPassWord, user.getSalt());
        if(!md5PassWord.equals(user.getPassword())){
            throw new UserNotFoundException("密码错误，请重新输入！");
        }

        String newMd5PassWord = getMd5PassWord(password, user.getSalt());
        Integer row = userMapper.modifyPassWord(newMd5PassWord, userName, userName, new Date());
        if(row!=1){
            throw new UpdateException("修改密码时出现未知异常");
        }

    }

    /**
     * 修改用户业务接口实现
     * @param user
     * @return
     */
    @Override
    public void modifyUserInfo(User user) {
        findByUser(user);
        user.setModifiedTime(new Date());
        user.setModifiedUser(user.getUsername());
        Integer row = userMapper.modifyUserInfo(user);
        if(row!=1){
            throw new UpdateException("修改用户时出现未知异常");
        }
    }

    /**
     * 实现用户信息业务接口
     * @param uid
     * @return
     */
    @Override
    public User findByUid(Integer uid) {
        return userMapper.findByUid(uid);
    }

    /**
     * 实现上传头像业务接口
     * @param uid
     * @param avatar
     * @param userName
     */
    @Override
    public void modifyAvatar(Integer uid, String avatar, String userName) {
        findByUser(uid);
        Integer row = userMapper.modifyAvatar(uid, avatar, userName, new Date());
        if(row!=1){
            throw new UpdateException("上传头像出现未知异常");
        }
    }

    /**
     * MD5密码加密
     * @param passWord
     * @param salt 盐值
     * @return
     */
    private String getMd5PassWord(String passWord, String salt){
        for (int i = 0; i < 3; i++) {
            passWord = DigestUtils.md5DigestAsHex((salt + passWord + salt).getBytes()).toUpperCase();
        }
        return passWord;
    }

}
